import {
  getStorage,
  setStorage,
  setData
} from '../../utils/util';
const dbU = wx.cloud.database().collection('User')
const dbM = wx.cloud.database().collection('Main')
const db = wx.cloud.database()
// component/search.js
module.exports = {
  init(that) {
    this._setData(that, {
      'searchList': getStorage('searchList') || []
    })
  },
  bindShowLog(e, that) {
    this.showlog(that);
    this._setData(that, {
      inputVal:''
    })
  },
  bindHideLog(e, that) {
    this._setData(that, {
      'searchIsHidden': true
    })
  },
  check_que(e, that) {
    this._setData(that, {
      search_type: '问题',
      activeNames: []
    })
  },
  check_vol(e, that) {
    this._setData(that, {
      search_type: '义工',
      activeNames: []
    })
  },
  onChange(e, that) {
    this._setData(that, {
      activeNames: e.detail,
    });
  },
  bindInputSchool(e, that) {
    var val = e.detail.value;
    this.matchStroage(that, val) //搜索历史searchList 操作
  },
  bindSearchAllShow(e, that) {
    this._setData(that, {
      searchAllShow: true
    })
  },
  bindGoSchool(e,that) {
    var val = e.currentTarget.dataset.item
    // console.log( that.data.tabData.searchList[index]);
    this._setData(that, {
      inputVal:val,
      searchIsHidden: true, //隐藏history和热词 跳到新页面
      searchResultIsHidden: false //显示搜索结果
    })
    this.goSchool(that, val)
  },
  bindGoSearch(e, that) {
    let searchList_stroage = getStorage('searchList') || [];
    const inputVal = that.data.tabData.inputVal;
    const len = searchList_stroage.length;
    var flag=1;
    for (var i = 0; i < len; i++){
      if(searchList_stroage[i] == inputVal){
        flag=0;
        break
      }
    }
   if(flag){searchList_stroage.push(inputVal)}

    setStorage('searchList', searchList_stroage)
    this._setData(that, {
      // inputVal: '',
      searchIsHidden: true, //隐藏history和热词 跳到新页面
      searchResultIsHidden: false //显示搜索结果
    })
    this.goSchool(that, inputVal)
  },
  bindDelLog(e, that) {
    let val = e.currentTarget.dataset.item;
    let searchList_stroage = getStorage('searchList') || [];
    let index = searchList_stroage.indexOf(val);
    searchList_stroage.splice(index, 1)
    this.updataLog(that, searchList_stroage)
  },
  bindSearchHidden(that) {
    this._setData(that, {
      wx: wx.navigateBack({})
    })
  },
  showlog(that) {
    let searchList_stroage = getStorage('searchList') || [];
    let searchList = []
    if (typeof (searchList_stroage) != undefined && searchList_stroage.length > 0) {
      for (var i = 0, len = searchList_stroage.length; i < len; i++) {
        searchList.push(searchList_stroage[i])
      }
    } else {
      searchList = searchList_stroage
    }
    this._setData(that, {
      searchIsHidden: false,
      searchResultIsHidden: true,
      searchAllShow: false,
      searchList
    })
  },

  // if (searchList_stroage[i] == val ) {
  //   i = len + 1
  // } else 
  matchStroage(that, val) {
    let searchList_stroage = getStorage('searchList') || [];
    let searchList = [];
    let len = searchList_stroage.length;
    if (typeof (val) != undefined && val.length > 0 && typeof (searchList_stroage) != undefined && searchList_stroage.length > 0) {
      for (var i = 0; i < len; i++) {
        if (searchList_stroage[i].indexOf(val) != -1) {
          searchList.push(searchList_stroage[i])
        }
      }
    } else {
      searchList = searchList_stroage
    }
    this._setData(that, {
      inputVal: val,
      searchList
    })
  },

  _setData(that, param) {
    let tabData = that.data.tabData;
    for (var key in param) {
      tabData[key] = param[key];
    }
    that.setData({
      tabData
    })
  },
  updataLog(that, list) {
    setStorage('searchList', list)
    this._setData(that, {
      searchList: list
    })
  },

  //search_result_list 赋值
  goSchool(that, val) {
    // console.log(that.data.tabData.search_type)
    if (that.data.tabData.search_type == '问题') {
      //获取问题榜
      dbM.where({
        sub: that.data.selected_subname,
        //使用正则查询，实现对搜索的模糊查询
        title: db.RegExp({
          //从搜索栏中获取的inputVal作为规则进行匹配
          regexp: val,
          options: 'i',
          //大小写不区分
        })
      }).get().then(res => {
        this._setData(that, {
          search_result_list: res.data
        })
      })
    };
    if (that.data.tabData.search_type == '义工') {
      //  if搜索的是义工  显示义工回答过的问题
      // 列出所有含关键字的义工
      //获取所有义工
      dbU.where({
        identity: 'W',
        name:db.RegExp({
          //从搜索栏中获取的inputVal作为规则进行匹配
          regexp: val,
          options: 'i',
          //大小写不区分
        }) 
      }).get().then(res => {
        this._setData(that, {
          search_result_list: res.data
        })
      })
    }
  },

  goSbPost: function (e,that) {
    let index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../../pages/post/post?id=' + that.data.tabData.search_result_list[index]._id
    })
  },

  goSbVol_list: function (e,that) {
    let index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '../../pages/vol_lst/vol_list?id=' + that.data.tabData.search_result_list[index]._openid + '&name=' + that.data.tabData.search_result_list[index].name
    })
  },
}